var exec = require('child_process').exec, child;
var client = require("../conf/redis").client;
var logger = require('../conf/log4').logger('crawlRank');
var mb = require('./mbHandler');
var rank = require('./rank');
var comm = require('./comment');
var utils = require('./utils');
var dao = require('./dao');

function crawlRank(rows,jobep) {
    for (var i in rows){
        client.lpush("jd_rank_keyword",JSON.stringify(rows[i]));
    }
    console.log(" rank keyword done.")
    if(jobep)jobep.emit('rank_job');
}

function crawlComm(rows,jobep) {
    for (var i in rows){
        client.lpush("jd_comment_sku",JSON.stringify(rows[i]));
    }
    console.log(" comm sku done.")
    if(jobep)jobep.emit('comm_job');
}

function crawlMbRank(rows) {
    for (var i in rows){
        client.lpush("jd_mb_rank_keyword",JSON.stringify(rows[i]));
    }
}

function execCmd(obj) {
    var cmd = "phantomjs ./crawl/jdRank.js '" + obj.keyword + "' " + obj.is_jd_deliver + " " + obj.category;
    child = exec(cmd);
    child.stdout.on('data', function (data) {
        data = data.trim();
        logger.info(data);
        if (data.startsWith("time")) {
            logger.info(" #### --- >finish.");
            loopJob();
        } else if (data.indexOf("timeout")>0) {
            var page = data.split("|")[1];
            logger.info("#### --- > timeout . page:"+page);
            retry(cmd+" "+page);
        }
    });
}

function retry(cmd) {
    logger.info(" ---- retry ---->"+cmd);
    child = exec(cmd);
    child.stdout.on('data', function (data) {
        data = data.trim();
        logger.info(data);
        if (data.startsWith("time")) {
            logger.info(" #### --- >finish.");
            loopJob();
        } else if (data.indexOf("timeout")>0) {
            logger.info("#### --- > timeout .");
            loopJob();
        }
    });
}

function loopJob() {
    client.lpop("jd_rank_keyword", function (err, res) {
        var obj = JSON.parse(res);
        logger.info(obj);
        if(res !== null){
            execCmd(obj);
        }
    });
}




function loopMbJob() {

    var EventProxy = require('eventproxy');
    var ep = new EventProxy();

    ep.after('next_job', 10, function (list) {
        var data = list[0];
        logger.info("----- next job ----->sort:"+data.sort+",zy:"+data.self+",keyword:"+data.keyword)
        loopMbJob();
    });


    client.lpop("jd_mb_rank_keyword", function (err, res) {
        var obj = JSON.parse(res);
        if(res !== null){
            for (var i = 1;i<11;i++){
                obj.pageNum = i;
                mb.loadMbRank(obj,ep);
            }
        }else{
            logger.info("---- hot key job done ----")
            ep.removeAllListeners();
            return;
        }
    });
}



function loopHotKeyRankJob(jobEp) {

    var EventProxy = require('eventproxy');
    var ep = new EventProxy();

    ep.on('next_word', function (keyword) {
        logger.info("----- next job ----->keyword:"+keyword)
        loopHotKeyRankJob(jobEp);
    });


    client.lpop("jd_rank_keyword", function (err, res) {
        var obj = JSON.parse(res);
        if(res !== null){
            rank.loadRank(obj.key,ep);
        }else{
            logger.info("---- loopHotKeyRankJob done ----")
            ep.removeAllListeners();
            jobEp.emit('comm_sku_job');
            return;
        }
    });
}


function loopCommentJob() {
    var EventProxy = require('eventproxy');
    var ep = new EventProxy();
    var page = 0,commTime = '2017-01-01';
    ep.on('next_word', function (keyword) {
        ep.removeAllListeners();
        logger.info("----- next next_word ----->keyword:"+keyword);
        client.hset("jd_comment_asin_hash",keyword,utils.getFetchTime(-1));
        loopCommentJob();
    });
    ep.on('next_page',function (keyword) {
        comm.loadComment(keyword,++page,commTime,ep);
    });

    client.lpop("jd_comment_sku", function (err, res) {
        if(res !== null){
            var obj = JSON.parse(res);

            //先查询之前有没记录，没有就把评论抓到1月1号
            client.hget("jd_comment_asin_hash",obj.sku_id,function (err1,time) {
                if(time) {
                    commTime = time;
                }
                console.log("sku:"+obj.sku_id+",page:"+page+",time"+commTime);
                comm.loadComment(obj.sku_id,page,commTime,ep);
            });


        }else{
            logger.info("---- loopCommentJob done ----")
		dao.updateCount();
		logger.info("---- update comm count done ----");
            ep.removeAllListeners();
            return;
        }
    });
}

exports.crawlRank = crawlRank;
exports.crawlComm = crawlComm;
exports.crawlMbRank = crawlMbRank;
exports.loopMbJob = loopMbJob;
exports.loopJob = loopJob;
exports.loopHotKeyRankJob = loopHotKeyRankJob;
exports.loopCommentJob = loopCommentJob;

function execAm() {
    var cmd = "phantomjs ../crawl/amRank.js ";
    child = exec(cmd);
    child.stdout.on('data', function (data) {
        data = data.trim();
        console.log(data);
    });
}

execAm();
